package com.vegetable.modules.controller.stock;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.stock.Stock;
import com.vegetable.modules.entity.stock.StockStatistics;
import com.vegetable.modules.entity.stock.po.*;
import com.vegetable.modules.entity.stock.to.StockCostChangeDTO;
import com.vegetable.modules.entity.stock.to.StockFlowDTO;
import com.vegetable.modules.service.stock.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存表 前端控制器
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-21
 */
@Slf4j
@RestController
@RequestMapping("/stock")
@AllArgsConstructor
@Api(value = "库存", tags = "库存")
public class StockController extends AbstractController {

    private IStockService stockService;
    private IStockFlowService stockFlowService;

    private IStockCheckService iStockCheckService;

    private IStockCostChangeService stockCostChangeService;
    private IStockStatisticsService stockStatisticsService;

    /**
     * 日期库存列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "条件查询日期库存列表", notes = "条件查询日期库存列表")
    public R list(StockQueryPO po){
        log.info("条件查询日期库存列表{}", JSON.toJSONString(po));
        PageUtils page = stockService.queryPage(po);
        return R.data( page);
    }

    /**
     * 查询分组商品库存列表
     */
    @GetMapping("/stockGroupBy")
    @ApiOperation(value = "查询分组商品库存列表", notes = "查询分组商品库存列表")
    public R stockGroupBy(@RequestParam Map<String, Object> params){
        log.info("查询分组商品库存列表{}", JSON.toJSONString(params));
        PageUtils page = stockService.stockGroupBy(params);
        return R.data( page);
    }

    @GetMapping("/stockList")
    @ApiOperation(value = "查询某商品库存列表", notes = "查询分组商品库存列表")
    public R stockList(@RequestParam(value = "commodityId")Long commodityId){
        log.info("查询{}商品库存列表", commodityId);
        List<Stock> stocks = stockService.stockList(commodityId);
        return R.data(stocks);
    }

    @PostMapping("/stockFlowList")
    @ApiOperation(value = "查询库存流水列表", notes = "查询分组商品库存流水列表", response = StockFlowDTO.class)
    public R stockFlowList(@RequestBody StockFlowQueryPO po){
        log.info("查询{}商品库存流水列表", po);
        Map<String, Object> result = stockFlowService.stockFlowLists(po);
        return R.data(result);
    }

    @PostMapping("/statisticsStockFlow")
    @ApiOperation(value = "统计库存流水列表", notes = "统计库存流水列表", response = StockFlowDTO.class)
    public R statisticsStockFlow(@RequestBody StockFlowQueryPO po){
        log.info("统计{}商品库存流水列表", po);
        IPage<StockFlowDTO> result = stockFlowService.statisticsStockFlow(po);
        return R.data(result);
    }

    @PostMapping("/statisticsStockFlow2")
    @ApiOperation(value = "统计库存流水列表", notes = "统计库存流水列表", response = StockFlowDTO.class)
    public R statisticsStockFlow2(@RequestBody StockFlowQueryPO po){
        log.info("统计{}商品库存流水列表", po);
        IPage<StockFlowDTO> result = iStockCheckService.statisticsStockFlow2(po);
        return R.data(result);
    }

    @PostMapping("/stockFlowStatistics")
    @ApiOperation(value = "查询库存流水统计", notes = "查询库存流水统计")
    public R stockFlowStatistics(@RequestBody StockFlowQueryPO po){
        log.info("查询{}商品库存流水列表", po);
        Map<String, Object> result = new HashMap<>();
        result.put("number", 34);
        result.put("cost", 223.43);
        return R.data(result);
    }

    @PostMapping("/updateStock")
    @ApiOperation(value = "查询某商品库存列表", notes = "查询分组商品库存列表")
    public R updateStock(@Valid @RequestBody StockUpdatePO po) throws Exception {
        log.info("查询{}商品库存列表", po);
        Integer result = stockService.updateStock(po);
        return R.data(result);
    }

    @PostMapping("/statistics")
    @ApiOperation(value = "库存汇总信息", notes = "库存汇总信息", response = StockStatistics.class)
    public R statistics(@RequestBody StockStatisticsQueryPO po){
        log.info("查询{}商品库存流水列表", po);
        IPage<StockStatistics> result = stockStatisticsService.stockStatisticsList(po);
        return R.data(result);
    }

    @PostMapping("/costChange")
    @ApiOperation(value = "查询库存成本变化", notes = "查询库存成本变化", response = StockCostChangeDTO.class)
    public R costChange(@RequestBody StockCostChangeQueryPO po){
        log.info("查询{}商品库存流水列表", po);
        IPage<StockCostChangeDTO> result = stockCostChangeService.stockCostChange(po);
        return R.data(result);
    }

    @PostMapping("/addCostChange")
    @ApiOperation(value = "新增其他库存费用", notes = "新增其他库存费用", response = StockCostChangeDTO.class)
    public R addCostChange(@RequestBody StockCostAddPO po) throws Exception {
        stockCostChangeService.StockCostAddPO(po);
        return R.data("result");
    }

    @PostMapping("/revokeCostChange")
    @ApiOperation(value = "撤销其他库存费用", notes = "撤销其他库存费用", response = StockCostChangeDTO.class)
    public R revokeCostChange(@RequestBody StockCostAddPO po) throws Exception {
        stockCostChangeService.revokeCostChange(po);
        return R.data("result");
    }

    @PostMapping("/changeCost")
    @ApiOperation(value = "修改库存成本", notes = "修改库存成本", response = StockCostChangeDTO.class)
    public R changeCost(@RequestBody StockCostChangeQueryPO po){
        IPage<StockCostChangeDTO> result = stockCostChangeService.stockCostChange(po);
        return R.data(result);
    }

    @GetMapping("/queryDetailStock")
    @ApiOperation(value = "走货需要查询的商品库存信息", notes = "走货需要查询的商品库存信息")
    public R list(Long id, boolean accurate, String commodityName, BigDecimal standard) throws Exception {
        List<String> noRoleUsers = Arrays.asList("f008", "f010", "f011", "f018", "f022", "f028");
        String currentUser = getUser().getUsername();
        if(noRoleUsers.contains(currentUser)){
            throw new Exception("当前操作人" + currentUser + "无权查看库存信息.");
        }
        List<Stock> page = stockService.queryDetailStock(id, accurate, commodityName, standard);
        return R.data( page);
    }
    
    /**
     * 查询订单时间超过两天的成品库存
     */
    @GetMapping("/finishedProductsWithOldOrderTime")
    @ApiOperation(value = "查询订单时间超过两天的成品库存", notes = "查询订单时间超过两天的成品库存")
    public R getFinishedProductsWithOldOrderTime(){
        log.info("查询订单时间超过两天的成品库存");
        List<Stock> stocks = stockService.getFinishedProductsWithOldOrderTime();
        return R.data(stocks);
    }
    
    /**
     * 分页查询订单时间超过两天的成品库存
     */
    @GetMapping("/finishedProductsWithOldOrderTimePage")
    @ApiOperation(value = "分页查询订单时间超过两天的成品库存", notes = "分页查询订单时间超过两天的成品库存")
    public R getFinishedProductsWithOldOrderTimeByConditionsPage(StockQueryPO po) {
        log.info("分页查询订单时间超过两天的成品库存，参数：{}", JSON.toJSONString(po));
        // 设置为超期成品查询
        po.setOverdueFinishedProduct(true);
        PageUtils page = stockService.getFinishedProductsWithOldOrderTimeByConditionsPage(po);
        return R.data(page);
    }
}